// @author Tjzlo
// @version 2025/2/21 09:46

package com.old.controller;

import com.old.dto.CheckStatusDTO;
import com.old.result.PageBean;
import com.old.result.Result;
import com.old.service.ActivityService;
import com.old.service.AdminService;
import com.old.service.PostService;
import com.old.service.VideoService;
import com.old.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin")
@RequiredArgsConstructor

public class AdminController {

    private final AdminService adminService;
    private final PostService postService;
    private final VideoService videoService;
    private final ActivityService activityService;

    //获取等待审核帖子
    @RequestMapping("/check/getPostList")
    public Result<PageBean> getPostList(@RequestParam(defaultValue = "1") Integer pageNo,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        @RequestParam (required = false)Integer sortBy,
                                        @RequestParam (required = false)String searchTerms){
        PageBean pageBean= adminService.getPostList(pageNo,pageSize,sortBy,searchTerms);
        log.info("分页查询: 页码={} 页大小={} 搜索词={} 排序方式={} 总数={}", pageNo, pageSize, searchTerms, sortBy, pageBean.getTotal());

        return Result.success(pageBean);
    }

    //获取等待审核帖子
    @RequestMapping("/check/getVideoList")
    public Result<PageBean> getVideoList(@RequestParam(defaultValue = "1") Integer pageNo,
                                        @RequestParam(defaultValue = "5") Integer pageSize,
                                        @RequestParam (required = false)Integer sortBy,
                                        @RequestParam (required = false)String searchTerms){
        PageBean pageBean= adminService.getVideoList(pageNo,pageSize,sortBy,searchTerms);
        log.info("分页查询: 页码={} 页大小={} 搜索词={} 排序方式={} 总数={}", pageNo, pageSize, searchTerms, sortBy, pageBean.getTotal());

        return Result.success(pageBean);
    }

    //获取等待审核活动
    @RequestMapping("/check/getActivityList")
    public Result<PageBean> getActivityList(@RequestParam(defaultValue = "1") Integer pageNo,
                                         @RequestParam(defaultValue = "5") Integer pageSize,
                                         @RequestParam (required = false)Integer sortBy,
                                         @RequestParam (required = false)String searchTerms){
        PageBean pageBean= adminService.getActivityList(pageNo,pageSize,sortBy,searchTerms);
        log.info("分页查询: 页码={} 页大小={} 搜索词={} 排序方式={} 总数={}", pageNo, pageSize, searchTerms, sortBy, pageBean.getTotal());

        return Result.success(pageBean);
    }

    //删除帖子
    //复用用户中心删除帖子逻辑
    @DeleteMapping("/delPost")
    public Result<String> deletePost(Integer postId,
                                     @RequestHeader(required = false,value = "Authorization") String token){
        // 1. 参数校验
        if (postId == null) {
            return Result.error("postId 不能为空");
        }

        try {
            // 2. 获取当前登录用户ID
            Integer userId = JwtUtil.getUserIdFromToken(token);
            if (userId == null) {
                return Result.error("用户未登录");
            }
            // 3. 删除帖子
            postService.deletePost(postId,userId);
            log.info("帖子删除成功，postId：{}", postId);
            return Result.success();
        } catch (Exception e) {
            log.error("帖子删除失败", e);
            return Result.error("帖子删除失败");
        }
    }

    //删除戏曲
    //复用用户中心删除戏曲逻辑
    @DeleteMapping("/delVideo")
    public Result deleteVideo(@RequestParam(required = false) Integer videoId,
                              @RequestHeader(required = false,value = "Authorization") String token) {
        // 1. 参数校验
        if (videoId == null) {
            return Result.error("videoId 不能为空");
        }

        try {
            // 2. 获取当前登录用户ID
            Integer userId = JwtUtil.getUserIdFromToken(token);
            if (userId == null) {
                return Result.error("用户未登录");
            }
            // 3. 删除戏曲
            videoService.deleteVideo(videoId,userId);
            log.info("戏曲删除成功，videoId：{}", videoId);
            return Result.success();
        } catch (Exception e) {
            log.error("戏曲删除失败", e);
            return Result.error("戏曲删除失败");
        }
    }

    //删除活动
    //复用用户中心删除活动逻辑
    @DeleteMapping("/delActivity")
    public Result deleteActivity(@RequestParam(required = false) Integer activityId) {
        // 1. 参数校验
        if (activityId == null) {
            return Result.error("activityId 不能为空");
        }

        try {
            // 2. 获取当前登录用户ID
//            Integer userId = getCurrentUserIdFromToken();
//            if (userId == null) {
//                return Result.error("用户未登录");
//            }

            // 3. 删除活动
            activityService.deleteActivity(activityId);
            log.info("活动删除成功，activityId：{}", activityId);
            return Result.success();
        } catch (Exception e) {
            log.error("活动删除失败", e);
            return Result.error("活动删除失败");
        }
    }

    //批量删除戏曲

    @DeleteMapping("/delVideoS")
    public Result deleteVideos(@RequestParam("ids") List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("参数不能为空");
        }
        videoService.deleteVideosByIds(ids);
        return Result.success();
    }
    //批量删除帖子
    @DeleteMapping("/delPostS")
    public Result deletePosts(@RequestParam("ids") List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("参数不能为空");
        }
        postService.deletePostsByIds(ids);
        return Result.success();
    }

    //批量删除活动
    @DeleteMapping("/delActivityS")
    public Result deleteActivities(@RequestParam("ids") List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("参数不能为空");
        }
        activityService.deleteActivitiesByIds(ids);
        return Result.success();
    }


    //审核通过帖子
    @PutMapping("/check/updatePostStatus")
    public Result updatePostStatus(@RequestBody CheckStatusDTO checkStatusDTO) {
        Integer postId=checkStatusDTO.getPostId();
        Integer status=checkStatusDTO.getStatus();

        // 0. 身份判断
        if (status != 0) {
            return Result.error("身份验证失败,不是管理员来这凑什么热闹");
        }

        // 1. 参数校验
        if (postId == null) {
            return Result.error("postId不能为空");
        }

        try {
            // 2. 更新帖子状态
            adminService.updatePostStatus(postId);
            log.info("帖子状态更新成功，postId：{}", postId);
            return Result.success();
       } catch (Exception e) {
            log.error("帖子状态更新失败", e);
            return Result.error("帖子状态更新失败");
        }
    }

    //审核通过视频
    @PutMapping("/check/updateVideoStatus")
    public Result updateVideoStatus(@RequestBody CheckStatusDTO checkStatusDTO) {
        Integer videoId=checkStatusDTO.getVideoId();
        Integer status=checkStatusDTO.getStatus();

        // 0. 身份判断
        if (status != 0) {
            return Result.error("身份验证失败,不是管理员来这凑什么热闹");
        }

        // 1. 参数校验
        if (videoId == null) {
            return Result.error("videoId不能为空");
        }

        try {
            // 2. 更新视频状态
            adminService.updateVideoStatus(videoId);
            log.info("视频状态更新成功，videoId：{}", videoId);
            return Result.success();
        }catch (Exception e) {
            log.error("视频状态更新失败", e);
            return Result.error("视频状态更新失败");
        }
    }

    //审核通过活动
    @PutMapping("/check/updateActivityStatus")
    public Result updateActivityStatus(@RequestBody CheckStatusDTO checkStatusDTO) {
        Integer activityId=checkStatusDTO.getActivityId();
        Integer status=checkStatusDTO.getStatus();

        // 0. 身份判断
        if (status != 0) {
            return Result.error("身份验证失败,不是管理员来这凑什么热闹");
        }

        // 1. 参数校验
        if (activityId == null) {
            return Result.error("activityId不能为空");
        }

        try {
            // 2. 更新活动状态
            adminService.updateActivityStatus(activityId);
            log.info("活动状态更新成功，activityId：{}", activityId);
            return Result.success();
        }catch (Exception e) {
            log.error("活动状态更新失败", e);
            return Result.error("活动状态更新失败");
        }
    }

    //内容管理-post
    @RequestMapping("/content/getPostList")
    public Result<PageBean> getPostListContent(@RequestParam(defaultValue = "1") Integer pageNo,
                                        @RequestParam(defaultValue = "5") Integer pageSize,
                                        @RequestParam (required = false)Integer sortBy,
                                        @RequestParam (required = false)String searchTerms)
    {
        PageBean pageList = adminService.getPostListContent(pageNo, pageSize, sortBy, searchTerms);
        log.info("分页查询: 页码={} 页大小={} 搜索词={} 排序方式={} 总数={}", pageNo, pageSize, searchTerms, sortBy, pageList.getTotal());
        return Result.success(pageList);
    }

    //内容管理-video
    @RequestMapping("/content/getVideoList")
    public Result<PageBean> getVideoListContent(@RequestParam(defaultValue = "1") Integer pageNo,
                                        @RequestParam(defaultValue = "5") Integer pageSize,
                                        @RequestParam (required = false)Integer sortBy,
                                        @RequestParam (required = false)String searchTerms)
    {
        PageBean pageList = adminService.getVideoListContent(pageNo, pageSize, sortBy, searchTerms);
        log.info("分页查询: 页码={} 页大小={} 搜索词={} 排序方式={} 总数={}", pageNo, pageSize, searchTerms, sortBy, pageList.getTotal());
        return Result.success(pageList);
    }

    //内容管理-activity
    @RequestMapping("/content/getActivityList")
    public Result<PageBean> getActivityListContent(@RequestParam(defaultValue = "1") Integer pageNo,
                                        @RequestParam(defaultValue = "5") Integer pageSize,
                                        @RequestParam (required = false)Integer sortBy,
                                        @RequestParam (required = false)String searchTerms)
    {
        PageBean pageList = adminService.getActivityListContent(pageNo, pageSize, sortBy, searchTerms);
        log.info("分页查询: 页码={} 页大小={} 搜索词={} 排序方式={} 总数={}", pageNo, pageSize, searchTerms, sortBy, pageList.getTotal());
        return Result.success(pageList);
    }


}
